home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2006 March
/
PCWMAR06.iso
/
Software
/
Freeware
/
Thingamablog 1.0.5
/
thinga-setup-1.0.5.exe
/
tb_legacy.jar
/
Blog.java
< prev
next >
Encoding:
Amiga
Atari
Commodore
DOS
FM Towns/JPY
Macintosh
Macintosh JP
Macintosh to JP
NeXTSTEP
RISC OS/Acorn
Shift JIS
UTF-8
Wrap
Java Source
|
2004-08-02
|
32.2 KB
|
1,073 lines
/*
* Copyright (C) 2003 Bob Tantlinger
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
import java.sql.*;
import java.util.*;
import java.io.*;
//import com.tantlinger.*;
public class Blog extends BlogDB implements Serializable
{
static final long serialVersionUID = 8562495511007798201L;
public static int ARCHIVE_MONTHLY = 0;
public static int ARCHIVE_WEEKLY = 1;
public static int ARCHIVE_BY_DAY_INTERVAL = 2;
private static final String ASC = "ASC";
private static final String DESC = "DESC";
public static final String RSS_FILENAME = "rss.xml";
private int archivePolicy = ARCHIVE_MONTHLY;
private String ascOrDesc = DESC;
private int daysInterval = 5;
private java.util.Date baseDate;
private boolean arcListAscending;
private boolean generateRss = true;
private boolean limitFrontPageEntries = true;
private int frontPageEntryLimit = 10;
private boolean limitCatPageEntries = true;
private int catPageEntryLimit = 10;
private Vector updatedPages = new Vector();
private DatedArchivePage curArcPage;
private File baseDir;
private File htmlDir;
private File arcsDir;
private File templateDir;
private File pageTmp;
private File arcTmp;
private File indexTmp;
private java.util.Date lastPublishDate;
private boolean isWriteFullRssEntries;
BlogVariables vars = new BlogVariables();
public Blog(File dir)
{
setBaseDate(new java.util.Date());
setBaseDirectory(dir);
}
public BlogVariables getBlogVariables()
{
return vars;
}
public void setBlogVariables(BlogVariables v)
{
vars = v;
}
public void setArchivePolicy(int p)
{
archivePolicy = p;
}
public int getArchivePolicy()
{
return archivePolicy;
}
public void setBaseDirectory(File dir)
{
DatedArchivePage.setArchiveRangeFormat(vars.getArchiveRangeFormat());
DatedArchivePage.setListAscending(arcListAscending);
if(dir == null || !dir.isDirectory())
return;
baseDir = dir;
dbDir = new File(baseDir.getAbsolutePath() + SEP + "DB");
htmlDir = new File(baseDir.getAbsolutePath() + SEP + "html");
arcsDir = new File(htmlDir.getAbsolutePath() + SEP + "archives");
templateDir = new File(baseDir.getAbsolutePath() + SEP + "templates");
if(!htmlDir.exists())
htmlDir.mkdir();
if(!arcsDir.exists())
arcsDir.mkdir();
if(!dbDir.exists())
dbDir.mkdir();
if(!templateDir.exists())
templateDir.mkdir();
setTemplateDirectory(templateDir);
}
public File getBaseDirectory()
{
return baseDir;
}
protected void addToUpdated(File f)
{
if(!updatedPages.contains(f))
updatedPages.add(f);
}
public File[] getUpdatedPages()
{
if(updatedPages.size() == 0)
return null;
File f[] = new File[updatedPages.size()];
for(int i = 0; i < f.length; i++)
f[i] = (File)updatedPages.elementAt(i);
return f;
}
public void removeUpdatedPage(File f)
{
updatedPages.remove(f);
}
public void clearUpdatedPages()
{
updatedPages.removeAllElements();
}
public void setTemplateDirectory(File d)
{
templateDir = d;
arcTmp = new File(templateDir.getAbsolutePath() + SEP + "archive.template");
pageTmp = new File(templateDir.getAbsolutePath() + SEP + "main.template");
indexTmp = new File(templateDir.getAbsolutePath() + SEP + "index.template");
}
public void setHtmlDirectory(File d)
{
htmlDir = d;
}
public void setArchiveDirectory(File d)
{
arcsDir = d;
}
public File getMainPage()
{
File f = new File(
getHtmlDirectory().getAbsolutePath() + SEP + vars.getMainPageFileName());
return f;
}
public File getMainPageTemplate()
{
return pageTmp;
}
public File getArchiveTemplate()
{
return arcTmp;
}
public File getIndexTemplate()
{
return indexTmp;
}
public File getHtmlDirectory()
{
return htmlDir;
}
public File getTemplateDirectory()
{
return templateDir;
}
public File getArchiveDirectory()
{
return arcsDir;
}
protected String readTemplate(File t) throws IOException
{
String template = "";
BufferedReader reader = new BufferedReader(new FileReader(t));
String line;
while((line = reader.readLine()) != null)
template += line + '\n';
reader.close();
return template;
}
protected void writeCategoryPages(String cats[]) throws IOException, SQLException
{
if(cats == null)
{
return;
}
for(int i = 0; i < cats.length; i++)
writeCategoryPage(cats[i]);
}
protected void writeCategoryPage(String cat) throws IOException, SQLException
{
File thePage = new CategoryPage(
getArchiveDirectory().getAbsolutePath(), cat);
String limit = "";
PreparedStatement ps;
if(limitCatPageEntries)
{
ps = conn.prepareStatement
("SELECT * FROM " + TABLE +
" WHERE " + CATEGORIES + " LIKE CONCAT('%', CONCAT(?, '%'))" +
" AND " + DRAFT + " = ? AND " + ARCPAGE + " <> ?");
ps.setString(1, "<" + cat + ">");
ps.setBoolean(2, false);
ps.setString(3, EXPIRED);
limit = computeLimitArgument(ps, catPageEntryLimit);
ps.close();
}
ps = conn.prepareStatement
("SELECT " + limit + " * FROM " + TABLE +
" WHERE " + CATEGORIES + " LIKE CONCAT('%', CONCAT(?, '%')) " +
" AND " + DRAFT + " = ? AND " + ARCPAGE + " <> ?" +
" ORDER BY " + TIMESTAMP + ' ' + ascOrDesc);
ps.setString(1, "<" + cat + ">");
ps.setBoolean(2, false);
ps.setString(3, EXPIRED);
ResultSet rs = ps.executeQuery();
BlogPageWriter pw = new BlogPageWriter(
new FileWriter(thePage), arcsDir, vars, readTemplate(arcTmp),
cat, BlogPageWriter.CAT_OR_ARC);
while(rs.next())
{
BlogEntryHeaderData hd = new BlogEntryHeaderData();
hd.setID(rs.getInt(ID));
hd.setTitle(rs.getString(TITLE));
hd.setTimestamp(rs.getTimestamp(TIMESTAMP));
hd.setAuthor(rs.getString(AUTHOR));
hd.setCategories(tokenizeCatString(rs.getString(CATEGORIES)));
hd.setLastModified(rs.getTimestamp(MODIFIED));
BlogEntry be = new BlogEntry(hd);
be.setEntryText(rs.getString(ENTRY));
pw.writeEntry(be, rs.getString(ARCPAGE));
}
pw.closePage();
pw.close();
ps.close();
addToUpdated(thePage);
}
protected void writeIndexPage() throws IOException
{
File f = new File(
getHtmlDirectory().getAbsolutePath() +SEP+ vars.getIndexPageFileName());
BlogPageWriter pw = new BlogPageWriter(
new FileWriter(f), arcsDir, vars, readTemplate(indexTmp), "",
BlogPageWriter.INDEX);
pw.closePage();
pw.close();
addToUpdated(f);
}
protected void writeArchivePage(String arcFileName) throws IOException, SQLException
{
String title = "";
try
{
File arcFile = new File(arcsDir.getAbsolutePath() +SEP+ arcFileName);
DatedArchivePage dap = DatedArchivePage.createDatedArchivePage(arcFile);
title = dap.getTextString();
}
catch(Exception ex){}
String query = "SELECT * FROM " + TABLE +
" WHERE " + ARCPAGE + " = " + "'" + arcFileName + "'" +
" AND " + DRAFT + " = '" + false + "'" +
" ORDER BY " + TIMESTAMP + ' ' + ascOrDesc;
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
File f = new File(arcsDir.getAbsolutePath() +SEP+ arcFileName);
BlogPageWriter pw = new BlogPageWriter(
new FileWriter(f), arcsDir, vars, readTemplate(arcTmp), title,
BlogPageWriter.CAT_OR_ARC);
while(rs.next())
{
BlogEntryHeaderData hd = new BlogEntryHeaderData();
hd.setID(rs.getInt(ID));
hd.setTitle(rs.getString(TITLE));
hd.setTimestamp(rs.getTimestamp(TIMESTAMP));
hd.setAuthor(rs.getString(AUTHOR));
hd.setCategories(tokenizeCatString(rs.getString(CATEGORIES)));
hd.setLastModified(rs.getTimestamp(MODIFIED));
BlogEntry be = new BlogEntry(hd);
be.setEntryText(rs.getString(ENTRY));
pw.writeEntry(be, rs.getString(ARCPAGE));
}
pw.closePage();
pw.close();
st.close();
addToUpdated(f);
}
protected void writeBlogPage() throws IOException, SQLException
{
Timestamp ts = new Timestamp(baseDate.getTime());
String limit = "";
if(limitFrontPageEntries)
{
PreparedStatement ps = conn.prepareStatement(
"SELECT * FROM " + TABLE +
" WHERE " + TIMESTAMP + " > ? AND " + DRAFT + " = ?" +
" AND " + ARCPAGE + " <> ?");
ps.setTimestamp(1, ts);
ps.setBoolean(2, false);
ps.setString(3, EXPIRED);
limit = computeLimitArgument(ps, frontPageEntryLimit);
ps.close();
}
String query = "SELECT " + limit + " * FROM " + TABLE +
" WHERE " + TIMESTAMP + " > " + "'" + ts + "' AND " +
DRAFT + " = '" + false + "' AND " +
ARCPAGE + " <> '" + EXPIRED + "'" +
" ORDER BY " + TIMESTAMP + ' ' + ascOrDesc;
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
File f = new File(getHtmlDirectory().getAbsolutePath() +SEP+ vars.getMainPageFileName());
BlogPageWriter pw = new BlogPageWriter(
new FileWriter(f), arcsDir, vars, readTemplate(pageTmp), "",
BlogPageWriter.FRONT_PAGE);
File rssFile = new File(assemblePath(
getHtmlDirectory().getAbsolutePath(), vars.getRssFileName()));
RSSWriter rssw = new RSSWriter(rssFile, vars, isWriteFullRssEntries);
if(isGenerateRss())
rssw.writeHeader();
while(rs.next())
{
BlogEntryHeaderData hd = new BlogEntryHeaderData();
hd.setID(rs.getInt(ID));
hd.setTitle(rs.getString(TITLE));
hd.setTimestamp(rs.getTimestamp(TIMESTAMP));
hd.setAuthor(rs.getString(AUTHOR));
hd.setCategories(tokenizeCatString(rs.getString(CATEGORIES)));
hd.setLastModified(rs.getTimestamp(MODIFIED));
BlogEntry be = new BlogEntry(hd);
be.setEntryText(rs.getString(ENTRY));
String arcPage = rs.getString(ARCPAGE);
pw.writeEntry(be, arcPage);
if(isGenerateRss())
rssw.writeEntry(be, arcPage);
}
pw.closePage();
pw.close();
if(isGenerateRss())
{
rssw.writeFooter();
addToUpdated(rssFile);
}
rssw.close();
st.close();
addToUpdated(f);
}
/** adds an entry to the DB */
public synchronized void addEntry(BlogEntry e) throws SQLException, IOException
{
boolean update = false;
String arcPage = "";
curArcPage = getMostRecentDatedArchivePage();
if(curArcPage == null ||
curArcPage.getExpirationDate().compareTo(e.getTimestamp()) < 0)
{
update = true;
}
else
arcPage = curArcPage.getName();
PreparedStatement ps = conn.prepareStatement
(
"INSERT INTO " + TABLE + "(" +
TIMESTAMP + ", " +
TITLE + ", " +
CATEGORIES + ", " +
ENTRY + ", " +
DRAFT + ", " +
MODIFIED + ", " +
AUTHOR + ", " +
ARCPAGE + ") " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?)"
);
ps.setTimestamp(1, e.getTimestamp());
ps.setString(2, e.getTitle());
ps.setString(3, catsString(e.getHeaderData().getCategories()));
ps.setString(4, e.getEntryText());
ps.setBoolean(5, e.getHeaderData().isDraft());
ps.setTimestamp(6, e.getHeaderData().getLastModified());
ps.setString(7, e.getHeaderData().getAuthor());
ps.setString(8, arcPage);
ps.executeUpdate();
ps.close();
if(update || e.isDraft())
{
System.out.println("Rebuilding Blog");
rebuildBlog();
}
else
{
System.out.println("Writing updated pages");
writeArchivePage(curArcPage.getName());
writeCategoryPages(e.getHeaderData().getCategories());
writeBlogPage();
}
}
public synchronized void updateEntry(BlogEntry be) throws SQLException, IOException
{
/*BlogEntry oldEntry = getEntry(be.getID());
super.updateEntry(be);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(
"SELECT * FROM " + TABLE + " WHERE ID = " + be.getID());
rs.next();
String apage = rs.getString(ARCPAGE);
st.close(); */
PreparedStatement ps = conn.prepareStatement
(
"UPDATE " + TABLE + " SET " +
TIMESTAMP + " = ?, " +
TITLE + " = ?, " +
CATEGORIES + " = ?, " +
ENTRY + " = ?, " +
DRAFT + " = ?, " +
MODIFIED + " = ?, " +
AUTHOR + " = ? WHERE " + ID + " = ?"
);
ps.setTimestamp(1, be.getTimestamp());
ps.setString(2, be.getTitle());
ps.setString(3, catsString(be.getHeaderData().getCategories()));
ps.setString(4, be.getEntryText());
ps.setBoolean(5, be.getHeaderData().isDraft());
ps.setTimestamp(6, be.getHeaderData().getLastModified());
ps.setString(7, be.getHeaderData().getAuthor());
ps.setInt(8, be.getID());
ps.executeUpdate();
ps.close();
rebuildBlog();
}
public long[] getAllEntryIDs() throws SQLException
{
Statement st = conn.createStatement();
Vector v = new Vector();
ResultSet rs = st.executeQuery("SELECT * FROM " + TABLE);
while(rs.next())
{
long id = rs.getInt(ID);
v.add(new Long(id));
}
st.close();
long ids[] = new long[v.size()];
for(int i = 0; i < ids.length; i++)
{
Long id = (Long)v.elementAt(i);
ids[i] = id.longValue();
}
return ids;
}
public synchronized void rebuildBlog() throws SQLException, IOException
{
DatedArchivePage.setArchiveRangeFormat(vars.getArchiveRangeFormat());
//expire entries less than baseDate
Timestamp base = new Timestamp(baseDate.getTime());
update("UPDATE " + TABLE + " SET " + ARCPAGE + "='" + EXPIRED + "' " +
"WHERE " + TIMESTAMP + " < " + "'" + base + "'");
//create archive pages for entries greater than baseDate based on arc policy
DatedArchivePage cur = null;
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(
"SELECT * FROM " + TABLE + " WHERE " + TIMESTAMP + " > " +
"'" + base + "' AND " + DRAFT + " <> 'true'" + " ORDER BY " + TIMESTAMP + " ASC");
Vector arcPages = new Vector();
while(rs.next())
{
Timestamp ts = rs.getTimestamp(TIMESTAMP);
//current record greater than current page date
if(cur == null || cur.getExpirationDate().compareTo(ts) < 0)
{
java.util.Date d1 = new java.util.Date(ts.getTime());
java.util.Date d2 = addIntervalToDate(ts, daysInterval - 1);
if(archivePolicy == ARCHIVE_MONTHLY)
{
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(d1);
cal.set(Calendar.DAY_OF_MONTH, 1);
d1 = cal.getTime();
cal.add(Calendar.MONTH, 1);
cal.add(Calendar.DATE, -1);
d2 = cal.getTime();
}
else if(archivePolicy == ARCHIVE_WEEKLY)
{
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(d1);
//cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
//bug fix - in some countries Monday is the 1st day of the week
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
d1 = cal.getTime();
//cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
cal.add(Calendar.DAY_OF_MONTH, 6);
d2 = cal.getTime();
//java.text.SimpleDateFormat mdf = new java.text.SimpleDateFormat("EEEE yyyy/MM/dd");
//System.out.println(mdf.format(d1) + " - " + mdf.format(d2));
}
cur = new DatedArchivePage(
getArchiveDirectory().getAbsolutePath(), d1, d2);
arcPages.add(cur);
}
int id = rs.getInt(ID);
update("UPDATE " + TABLE +
" SET " + ARCPAGE + "='" + cur.getName() +
"' WHERE " + ID + "=" + id);
}
st.close();
clearUpdatedPages();
//delete the old archive pages
File oldArcs[] = DatedArchivePage.listDatedArchivePages(getArchiveDirectory());
try
{
for(int i = 0; i < oldArcs.length; i++)
oldArcs[i].delete();
}
catch(Exception ex)
{
//System.out.println("Couldn't delete archvie file");
}
//create blank arc pages so we know what to list
try
{
for(int i = 0; i < arcPages.size(); i++)
((File)arcPages.elementAt(i)).createNewFile();
}
catch(Exception ex){}
//write the archive pages
for(int i = 0; i < arcPages.size(); i++)
{
File f = (File)arcPages.elementAt(i);
writeArchivePage(f.getName());
}
try{
curArcPage = (DatedArchivePage)arcPages.lastElement();
}catch(Exception ex){}
String categories[] = vars.getCategories();
for(int i = 0; i < categories.length; i++)
{
CategoryPage cp =
new CategoryPage(getArchiveDirectory().getAbsolutePath(),
categories[i]);
try{cp.delete();}
catch(Exception ex){}
writeCategoryPage(categories[i]);
}
writeIndexPage();
writeBlogPage();
}
public synchronized BlogEntryHeaderData[] getCurrentEntries() throws SQLException
{
Timestamp ts = new Timestamp(baseDate.getTime());
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(
"SELECT * FROM " + TABLE +
" WHERE " + TIMESTAMP + " > " + "'" + ts + "'" +
" AND " + DRAFT + " = '" + false + "'" +
" ORDER BY " + TIMESTAMP + " DESC");
BlogEntryHeaderData hd[] = listHeaders(rs);
st.close();
if(limitFrontPageEntries && (hd.length > frontPageEntryLimit))
{
BlogEntryHeaderData lhd[] = new BlogEntryHeaderData[frontPageEntryLimit];
for(int i = 0; i < lhd.length; i++)
lhd[i] = hd[i];
return lhd;
}
return hd;
}
public synchronized BlogEntryHeaderData[] getExpiredEntries() throws SQLException
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(
"SELECT * FROM " + TABLE +
" WHERE " + ARCPAGE + " = " + "'" + EXPIRED + "'" +
" AND " + DRAFT + " = '" + false + "'" +
" ORDER BY " + TIMESTAMP + " DESC");
BlogEntryHeaderData hd[] = listHeaders(rs);
st.close();
return hd;
}
public synchronized BlogEntryHeaderData[] getDrafts() throws SQLException
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(
"SELECT * FROM " + TABLE +
" WHERE " + DRAFT + " = " + "'" + true + "'" +
" ORDER BY " + TIMESTAMP + " DESC");
BlogEntryHeaderData hd[] = listHeaders(rs);
st.close();
return hd;
}
public synchronized void deleteEntries(int ids[]) throws SQLException, IOException
{
if(ids == null) return;
for(int i = 0; i < ids.length; i++)
update("DELETE FROM " + TABLE + " WHERE ID = " + ids[i]);
rebuildBlog();
}
public synchronized void deleteEntry(int id) throws SQLException, IOException
{
int ids[] = new int[1];
ids[0] = id;
deleteEntries(ids);
}
protected String computeLimitArgument(PreparedStatement query, int entryLimit)
throws SQLException
{
ResultSet rs = query.executeQuery();
int count = 0;
while(rs.next())
{
count++;
}
String limit = "";
if(count > entryLimit)
{
if(isWriteAscending())
limit = "LIMIT " + (count - entryLimit) + " " + entryLimit;
else
limit = "LIMIT " + "0 " + entryLimit;
}
return limit;
}
protected java.util.Date addIntervalToDate(java.util.Date date, int i)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, i);
return cal.getTime();
}
public java.util.Date getBaseDate()
{
return baseDate;
}
public void setBaseDate(java.util.Date d)
{
//set to 12:00 AM
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
baseDate = cal.getTime();
}
public void setDaysArchiveInterval(int i)
{
if(i > 0)
daysInterval = i;
}
public int getDaysArchiveInterval()
{
return daysInterval;
}
public void setGenerateRss(boolean b)
{
generateRss = b;
if(!generateRss)
{
File f = new File(
assemblePath(getHtmlDirectory().getAbsolutePath(),
RSS_FILENAME));
removeUpdatedPage(f);
f.delete();
}
}
public void setWriteFullRssEntries(boolean b)
{
isWriteFullRssEntries = b;
}
public boolean isWriteFullRssEntries()
{
return isWriteFullRssEntries;
}
public boolean isGenerateRss()
{
return generateRss;
}
public void setLimitFrontPageEntries(boolean b)
{
limitFrontPageEntries = b;
}
public boolean isLimitFrontPageEntries()
{
return limitFrontPageEntries;
}
public void setFrontPageEntryLimit(int l)
{
if(l > 0) frontPageEntryLimit = l;
}
public int getFrontPageEntryLimit()
{
return frontPageEntryLimit;
}
public void setArchiveListAscending(boolean b)
{
arcListAscending = b;
DatedArchivePage.setListAscending(b);
}
public boolean isArchiveListAscending()
{
return arcListAscending;
}
public void setLimitCategoryPageEntries(boolean b)
{
limitCatPageEntries = b;
}
public boolean isLimitCategoryPageEntries()
{
return limitCatPageEntries;
}
public void setCategoryPageEntryLimit(int l)
{
if(l > 0)catPageEntryLimit = l;
}
public int getCategoryPageEntryLimit()
{
return catPageEntryLimit;
}
public boolean isWriteAscending()
{
return ascOrDesc.equals(ASC);
}
public void setWriteAscending(boolean b)
{
if(b)
ascOrDesc = ASC;
else
ascOrDesc = DESC;
}
public void addCategory(String cat)
{
vars.addCategory(cat);
}
public void addAuthor(Author a)
{
vars.addAuthor(a);
}
public synchronized void renameCategory(String oldCat, String newCat) throws SQLException
{
String ca[] = vars.getCategories();
boolean contains = false;
for(int i = 0; i < ca.length; i++)
{
if(ca[i].equals(oldCat))
{
contains = true;
break;
}
}
if(!contains)
return;
PreparedStatement ps = conn.prepareStatement
("SELECT * FROM " + TABLE +
" WHERE " + CATEGORIES + " LIKE CONCAT('%', CONCAT(?, '%'))");
//String theCat = "<" + oldCat + ">";
ps.setString(1, "<" + oldCat + ">");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
String c = rs.getString(CATEGORIES);
int id = rs.getInt(ID);
int s = c.indexOf(oldCat);
int e = s + oldCat.length();
StringBuffer sb = new StringBuffer(c);
sb.replace(s, e, newCat);
//System.out.println(sb.toString());
PreparedStatement update = conn.prepareStatement
(
"UPDATE " + TABLE + " SET " +
CATEGORIES + " = ? " +
"WHERE " + ID + " = ?"
);
update.setString(1, sb.toString());
update.setInt(2, id);
update.executeUpdate();
update.close();
}
ps.close();
vars.removeCategory(oldCat);
vars.addCategory(newCat);
CategoryPage cp = new CategoryPage(
getArchiveDirectory().getAbsolutePath(), oldCat);
if(cp.exists())
cp.delete();
removeUpdatedPage(cp);
}
public synchronized void removeCategory(String cat) throws SQLException
{
PreparedStatement ps = conn.prepareStatement
("SELECT * FROM " + TABLE +
" WHERE " + CATEGORIES + " LIKE CONCAT('%', CONCAT(?, '%'))");
String theCat = "<" + cat + ">";
ps.setString(1, theCat);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
String c = rs.getString(CATEGORIES);
int id = rs.getInt(ID);
int s = c.indexOf(theCat);
// + 1 gets rid of trailing space
// seems kind of dangerous, but it works...
int e = s + theCat.length() + 1;
StringBuffer sb = new StringBuffer(c);
sb.delete(s, e);
//System.out.println(sb.toString());
PreparedStatement update = conn.prepareStatement
(
"UPDATE " + TABLE + " SET " +
CATEGORIES + " = ? " +
"WHERE " + ID + " = ?"
);
update.setString(1, sb.toString());
update.setInt(2, id);
update.executeUpdate();
update.close();
}
ps.close();
vars.removeCategory(cat);
CategoryPage cp = new CategoryPage(
getArchiveDirectory().getAbsolutePath(), cat);
if(cp.exists())
cp.delete();
removeUpdatedPage(cp);
}
public void removeAuthor(Author a) throws SQLException
{
PreparedStatement ps = conn.prepareStatement
("SELECT * FROM " + TABLE +
" WHERE " + AUTHOR + " = ?");
ps.setString(1, a.getName());
ResultSet rs = ps.executeQuery();
while(rs.next())
{
int id = rs.getInt(ID);
PreparedStatement update = conn.prepareStatement
(
"UPDATE " + TABLE + " SET " +
AUTHOR + " = ? " +
"WHERE " + ID + " = ?"
);
update.setString(1, "");
update.setInt(2, id);
update.executeUpdate();
update.close();
}
ps.close();
vars.removeAuthor(a.getName());
}
public void editAuthor(Author oldAuth, Author newAuth) throws SQLException
{
if(oldAuth.getName().equals(newAuth.getName()))
{
vars.addAuthor(newAuth); //update
return;
}
PreparedStatement ps = conn.prepareStatement
("SELECT * FROM " + TABLE +
" WHERE " + AUTHOR + " = ?");
ps.setString(1, oldAuth.getName());
ResultSet rs = ps.executeQuery();
while(rs.next())
{
int id = rs.getInt(ID);
PreparedStatement update = conn.prepareStatement
(
"UPDATE " + TABLE + " SET " +
AUTHOR + " = ? " +
"WHERE " + ID + " = ?"
);
update.setString(1, newAuth.getName());
update.setInt(2, id);
update.executeUpdate();
update.close();
}
ps.close();
vars.removeAuthor(oldAuth.getName());
vars.addAuthor(newAuth);
}
public String[] getCategories()
{
return vars.getCategories();
}
public Author[] getAuthors()
{
return vars.getAuthors();
}
public DatedArchivePage[] getDatedArchivePages()
{
return DatedArchivePage.listDatedArchivePages(getArchiveDirectory());
}
public DatedArchivePage getMostRecentDatedArchivePage()
{
if(curArcPage == null)
{
try
{
//TODO Fix so that an Asc/desc arc list returns the right page
DatedArchivePage p[] =
DatedArchivePage.listDatedArchivePages(getArchiveDirectory());
return p[p.length - 1]; //get the most recent page
}
catch(Exception ex){}
}
return curArcPage;
}
public void setLastPublishDate(java.util.Date d)
{
lastPublishDate = d;
}
public java.util.Date getLastPublishDate()
{
return lastPublishDate;
}
private String assemblePath(String p1, String p2)
{
String sep = System.getProperty("file.separator");
if(!p1.endsWith(sep))
p1 += sep;
return p1 + p2;
}
}